// AWTButton.cpp: implementation of the AWTButton class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTButton.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTButton, AWTComponent)

AWTButton::AWTButton()
{
	construct(_T(""));
}

AWTButton::AWTButton(LPTSTR label)
{
	construct(CString(label));
}

AWTButton::AWTButton(CString label)
{
	construct(label);
}

AWTButton::~AWTButton()
{
	if (m_actionListener != NULL)
	{
		if (m_actionListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_actionListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_actionListener);
#endif
			delete mc;
		}
	}
}

CWnd* AWTButton::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	CButton* pwnd = new AWTButtonPeer(this);
	((AWTButtonPeer*)pwnd)->setComponent(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE;
	if (m_default)
		dwStyle |= BS_DEFPUSHBUTTON;
	pwnd->Create(m_label, dwStyle, getBounds(), wParent, 0);
	pwnd->SetFont(CFont::FromHandle(m_font));
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize AWTButton::getMinimumSize()
{
	return getPreferredSize();
}

CSize AWTButton::getPreferredSize()
{
	if (m_peer == NULL)
		return CSize(0,0);
	CButton* pwnd = (CButton*)m_peer;
	CString s;
	pwnd->GetWindowText(s);
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	CSize sz = pdc->GetTextExtent(s);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx += 12;
	sz.cy += 8;
	return sz;
}

void AWTButton::processActionEvent(AWTActionEvent* e)
{
  if (m_actionListener != NULL) 
		m_actionListener->actionPerformed(e);
}

void AWTButton::setDefault(BOOL b)
{
	if (b == m_default)
		return;
	m_default = b;
	if (m_peer != NULL)
		((AWTButtonPeer*)m_peer)->setDefault(b);
}
